/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConnectionPane.java
 *
 * Created on 16.3.2011, 13:39:40
 */
package cz.muni.fi.rum.gui.component;

import cz.muni.fi.rum.domainmodel.Reflector;
import cz.muni.fi.rum.gui.MyDoggyController;
import cz.muni.fi.rum.reflector.telnet.TelnetServer;
import cz.muni.fi.rum.reflector.telnet.TelnetServerConections;
import cz.muni.fi.rum.sender.Factory;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractListModel;
import org.jdesktop.application.Action;

/**
 *
 * @author pmikulasek
 */
public class ConnectionPane extends javax.swing.JPanel {

    private static ConnectionPane instance;

    /** Creates new form ConnectionPane */
    private ConnectionPane() {
        initComponents();
    }

    public static ConnectionPane getInstance() {
        if (instance == null) {
            instance = new ConnectionPane();
        }
        return instance;
    }

    @Action
    public void connectAction() {
        TelnetServer server = (TelnetServer) serverList.getSelectedValue();
        try {
            Factory.getSenderManager().createAndConnectNewMessageSender(server);
            MyDoggyController.getInstance().showBaseDesktop(new Reflector());
        } catch (IOException ex) {
            Logger.getLogger(ConnectionPane.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Action
    public void connectDemoAction() {
        try {
            Factory.getSenderManager().createAndConnectNewMessageSender(null);
            MyDoggyController.getInstance().showBaseDesktop(new Reflector());
        } catch (IOException ex) {
            Logger.getLogger(ConnectionPane.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ConnectionPane = new javax.swing.JPanel();
        connectButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        serverList = new javax.swing.JList();
        demoButton = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cz.muni.fi.rum.RumConfigurator.class).getContext().getResourceMap(ConnectionPane.class);
        ConnectionPane.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("ConnectionPane.border.title"), javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 0, 11), resourceMap.getColor("ConnectionPane.border.titleColor"))); // NOI18N
        ConnectionPane.setName("ConnectionPane"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(cz.muni.fi.rum.RumConfigurator.class).getContext().getActionMap(ConnectionPane.class, this);
        connectButton.setAction(actionMap.get("connectAction")); // NOI18N
        connectButton.setText(resourceMap.getString("connectButton.text")); // NOI18N
        connectButton.setName("connectButton"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        serverList.setModel(new AbstractListModel() {

            @Override
            public int getSize() {
                return TelnetServerConections.getInstance().getAllServers().size();
            }

            @Override
            public Object getElementAt(int index) {
                return TelnetServerConections.getInstance().getAllServers().get(index);
            }
        });
        serverList.setName("serverList"); // NOI18N
        serverList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                serverListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(serverList);

        demoButton.setAction(actionMap.get("connectDemoAction")); // NOI18N
        demoButton.setText(resourceMap.getString("demoButton.text")); // NOI18N
        demoButton.setName("demoButton"); // NOI18N

        javax.swing.GroupLayout ConnectionPaneLayout = new javax.swing.GroupLayout(ConnectionPane);
        ConnectionPane.setLayout(ConnectionPaneLayout);
        ConnectionPaneLayout.setHorizontalGroup(
            ConnectionPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ConnectionPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ConnectionPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                    .addGroup(ConnectionPaneLayout.createSequentialGroup()
                        .addComponent(demoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(connectButton, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)))
                .addContainerGap())
        );
        ConnectionPaneLayout.setVerticalGroup(
            ConnectionPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ConnectionPaneLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(ConnectionPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(connectButton)
                    .addComponent(demoButton)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ConnectionPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(ConnectionPane, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void serverListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_serverListMouseClicked
        if (evt.getClickCount() == 2) {
            connectAction();
        }
    }//GEN-LAST:event_serverListMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel ConnectionPane;
    private javax.swing.JButton connectButton;
    private javax.swing.JButton demoButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList serverList;
    // End of variables declaration//GEN-END:variables
}
